package com.bootdo.collection.controller;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.collection.domain.PicJsonDO;
import com.bootdo.collection.domain.PictureModelDO;
import com.bootdo.collection.service.impl.PictureModelService;
import com.bootdo.collection.utils.JedisUtil;
import com.bootdo.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.*;

import java.util.zip.GZIPInputStream;

@Controller
@ResponseBody
@RequestMapping("/picServer")
public class PicServerController {
    /**
     * 客户端请求该接口,获取图片流并且保存到redis中
     *
     * @param request
     */
    @PostMapping("/savePicServer")
    public void getAllPictureSet(HttpServletRequest request) {
        InputStream inputStream = null;
        try {
            Part part = request.getPart("picJsonDO");
            //从Part对象中以流的方式获取参数
            inputStream = part.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
        JedisUtil.init();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPInputStream gzip = null;
        try {
            //将压缩的图片流解压缩
            gzip = new GZIPInputStream(inputStream);
            byte[] buf = new byte[1024];
            int num;
            baos = new ByteArrayOutputStream();
            while ((num = gzip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            PicJsonDO picJsonDO = JSONObject.parseObject(baos.toByteArray(), PicJsonDO.class);
            JedisUtil.hset(String.valueOf(picJsonDO.getPictureSetId()), "field2", picJsonDO.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
                if (gzip != null) {
                    gzip.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @GetMapping("/getPicJsonDO")
    public String getPictureSet() {
        JedisUtil.init();
        if (JedisUtil.exists("picJsonDO")) {
            return JedisUtil.rpop("picJsonDO");
        } else {
            return "";
        }
    }

    @PostMapping("/updatePicServer")
    @ResponseBody
    public R updatePicture(String pictureModelDOJson) {
//        redisTemplate.convertAndSend("PICJSON",pictureModelDOJson);
        JedisUtil.init();
        JedisUtil.lpush("PICJSON", pictureModelDOJson);
//        PictureModelDO pictureModeDO = JSONObject.parseObject(pictureModelDOJson,PictureModelDO.class);
//        pictureModelService.add(pictureModeDO,pictureModeDO.getStationId());
        return R.ok();
    }

}
